﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using SD.Database.Classes;
using SD.Database;

namespace smallDeimos.Game
{
    public partial class Trade : System.Web.UI.Page
    {
        int UserId;
        protected void Page_Load(object sender, EventArgs e)
        {
            string b = "", s = "";
            if (Page.IsPostBack == true)
            {
                b = BuyList.SelectedValue;
                s = SellList.SelectedValue;
            }
            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
            //Load cash from the DB
            Label1.Text = "You account balance is " + String.Format("{0:F2}", UserFunctions.GetPlayerFloat(UserId, "Cash")) + "$";
            SuOwned.Text = UserFunctions.GetPlayerInt(UserId, "SupplyUnits").ToString();
            UpdateBuldings();
            UpdateArtifacts();
            if (Page.IsPostBack == true)
            {
                if (b != "")
                    BuyList.SelectedValue = b;
                if (s != "")
                    SellList.SelectedValue = s;
            }
            if (UserFunctions.UsedArtifact(UserId) == string.Empty)
                Label2.Text = "You don't use any artifacts";
            else
                Label2.Text = "You are using " + ArtifactsFunctions.ArtifactName(UserFunctions.UsedArtifact(UserId)) + ". " + ArtifactsFunctions.ArtifactDescription(UserFunctions.UsedArtifact(UserId));

            Label3.Text = "Supply Units($" + SD.Database.Configuration.GetConfigurationForOption("SupplyUnitsPrice") + ")";
        }

        private void UpdateArtifacts()
        {
            SellList.Items.Clear();
            BuyList.Items.Clear();
            List<string> tmp = UserFunctions.PlayerArtifacts(UserId);
            foreach (string t in tmp)
                SellList.Items.Add(new ListItem(ArtifactsFunctions.ArtifactName(t) + " $" + ArtifactsFunctions.Cost(t), t));
            tmp = UserFunctions.PossibleArtifacts(UserId);
            foreach (string t in tmp)
                BuyList.Items.Add(new ListItem(ArtifactsFunctions.ArtifactName(t) + " $" + ArtifactsFunctions.Cost(t), t));

        }

        private void UpdateBuldings()
        {
            //spy academy
            string status = UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.SpyAcademy) ? "Working" : "Not present";
            BuySpy.Enabled = !UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.SpyAcademy);
            SpyLbl.Text = status;
            //orbital center
            status = UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.OrbitalCC) ? "Working" : "Not present";
            BuyCenter.Enabled = !UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.OrbitalCC);
            CenterLbl.Text = status;
            //science network
            status = UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.ScienceNetwork) ? "Working" : "Not present";
            SciLblb.Text = status;
            BuySci.Enabled = !UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.ScienceNetwork);
            //Alliance cc
            status = UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.AllianceCC) ? "Working" : "Not present";
            AllLbl.Text = status;
            BuyAllianceCC.Enabled = !UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.AllianceCC);
            //market
            status = UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.Market) ? "Working" : "Not present";
            MarketLBL.Text = status;
            Button2.Enabled = !UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.Market);
        }
        private void BuyBulding(SD.Database.SpecialBuldings specialBuldings)
        {
            StoredProcedures.BuyBulding(UserId, specialBuldings);
            Response.Redirect(Page.Request.Url.ToString());
        }
        #region events
        protected void Button1_Click(object sender, EventArgs e)
        {
            CashFunctions.SendMoneyToCapital(UserId);
            Response.Redirect(Page.Request.Url.ToString());
        }

        protected void BuySpy_Click(object sender, EventArgs e)
        {
            BuyBulding(SD.Database.SpecialBuldings.SpyAcademy);
        }

        protected void BuyCenter_Click(object sender, EventArgs e)
        {
            BuyBulding(SD.Database.SpecialBuldings.OrbitalCC);
        }

        protected void BuySci_Click(object sender, EventArgs e)
        {
            BuyBulding(SD.Database.SpecialBuldings.ScienceNetwork);
        }

        protected void BuyAllianceCC_Click(object sender, EventArgs e)
        {
            BuyBulding(SD.Database.SpecialBuldings.AllianceCC);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            BuyBulding(SD.Database.SpecialBuldings.Market);
        }
        #endregion

        protected void BtnBuy_Click(object sender, EventArgs e)
        {
            if (BuyList.SelectedItem != null)
                StoredProcedures.BuyArtifact(UserId, BuyList.SelectedValue);
            //Response.Write(BuyList.SelectedValue);
            Response.Redirect(Page.Request.Url.ToString());
        }

        protected void BtnSell_Click(object sender, EventArgs e)
        {
            if (SellList.SelectedItem != null)
                StoredProcedures.SellArtifact(UserId, SellList.SelectedValue);
            Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (SellList.SelectedItem != null)
                StoredProcedures.UseArtifact(UserId, SellList.SelectedValue);
            Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            string tmp = SuBuy.Text;
            Response.Write("." + tmp + ".");
            try
            {
                int t = int.Parse(tmp);
                StoredProcedures.BuySu(UserId, t);

                Response.Redirect(Page.Request.Url.ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            StoredProcedures.SellSu(UserId, int.Parse(SuBuy.Text));
            Response.Write(SuBuy.Text);
            Response.Redirect(Page.Request.Url.ToString());

        }

    }
}
